var layer;
var imageFront;
var imageBack;
var imageHold;
var loading = false;
var bankCode;


$(function() {
	layui.use('layer', function() {
		layer = layui.layer;
	});

	getUserInfo();
	getUserBank();
	getBankList();

})

function cardBackChane(event) {
	var files = event.target.files,
		file;
	if (files && files.length > 0) {
		// 获取目前上传的文件
		file = files[0]; // 文件大小校验的动作
		if (file.size > 1024 * 1024 * 2) {
			alert('图片大小不能超过 2MB!');
			return false;
		}
		// 获取 window 的 URL 工具
		var URL = window.URL || window.webkitURL;
		// 通过 file 生成目标 url
		var imgURL = URL.createObjectURL(file);
		//用attr将img的src属性改成获得的url
		$("#card_back").attr("src", imgURL);
		$("#card_back").show();
		// 使用下面这句可以在内存中释放对此 url 的伺服，跑了之后那个 URL 就无效了
		// URL.revokeObjectURL(imgURL);
	}
}

function holdImgChange(event) {
	var files = event.target.files,
		file;
	if (files && files.length > 0) {
		// 获取目前上传的文件
		file = files[0]; // 文件大小校验的动作
		if (file.size > 1024 * 1024 * 2) {
			alert('图片大小不能超过 2MB!');
			return false;
		}
		// 获取 window 的 URL 工具
		var URL = window.URL || window.webkitURL;
		// 通过 file 生成目标 url
		var imgURL = URL.createObjectURL(file);
		//用attr将img的src属性改成获得的url
		$("#hold_img").attr("src", imgURL);
		$("#hold_img").show();
		// 使用下面这句可以在内存中释放对此 url 的伺服，跑了之后那个 URL 就无效了
		// URL.revokeObjectURL(imgURL);
	}
}

function filechange(event) {
	var files = event.target.files,
		file;
	if (files && files.length > 0) {
		// 获取目前上传的文件
		file = files[0]; // 文件大小校验的动作
		if (file.size > 1024 * 1024 * 2) {
			alert('图片大小不能超过 2MB!');
			return false;
		}
		// 获取 window 的 URL 工具
		var URL = window.URL || window.webkitURL;
		// 通过 file 生成目标 url
		var imgURL = URL.createObjectURL(file);
		//用attr将img的src属性改成获得的url
		$("#card_front").attr("src", imgURL);
		$("#card_front").show();
		// 使用下面这句可以在内存中释放对此 url 的伺服，跑了之后那个 URL 就无效了
		// URL.revokeObjectURL(imgURL);
	}
}

function getUserInfo() {
	$.ajax({
		type: "get",
		url: "http://localhost:8810/loan/app/user/userInfo",
		dataType: "json",
		async: false,
		//这里是重点
		beforeSend: setHeader,
		success: function(data) {
			var userEntity = data.data.user;
			$("#card_front").attr('src',userEntity.cardFront);
			$("#card_back").attr('src',userEntity.cardBack);
			$("#hold_img").attr('src',userEntity.holdImg);
			 imageFront = userEntity.cardFront;
			 imageBack = userEntity.cardBack;
			 imageHold = userEntity.holdImg;
		},
		error: function(error) {
			layer.msg(error.message);
		}
	})
}

function getBankList() {
	$.ajax({
		type: "get",
		url: "http://localhost:8810/loan/app/bank/getList",
		dataType: "json",
		async: false,
		//这里是重点
		beforeSend: setHeader,
		success: function(data) {
			if (data.code == 0) {
				$("#bankId").empty();
				$("#bankId").append('<option value="" class="scvbn"></option>');
				$.each(data.list, function(index, item) {
					if (bankCode == item.bankNo) {
						$("#bankId").append('<option value="' + item.bankNo + '" class="scvbn" selected="selected">' + item.bankName +
							'</option>');
					} else {
						$("#bankId").append('<option value="' + item.bankNo + '" class="scvbn">' + item.bankName + '</option>');
					}
				})
			}
		},
		error: function(error) {
			layer.msg(error.message);
		}
	})
}

function getUserBank() {
	$.ajax({
		type: "get",
		url: "http://localhost:8810/loan/app/user/getUserBank",
		dataType: "json",
		async: false,
		//这里是重点
		beforeSend: setHeader,
		success: function(data) {
			if (data.code == 0  && data.data.userBank) {
				$("#bankName").val(data.data.userBank.bankName);
				$("#bankNo").val(data.data.userBank.bankNo);
				bankCode = data.data.userBank.bankCode;
			}
		},
		error: function(error) {
			layer.msg(error.message);
		}
	})
}

function submit() {
	if (loading) {
		return false;
	}
	loading = true;
	var bankName = $("#bankName").val();
	var bankId = $("#bankId").val();
	var bankNo = $("#bankNo").val();
	var cardFront = $("#card_front").attr('src');
	var cardBack = $("#card_back").attr('src');
	var holdImg = $("#hold_img").attr('src');

	if ($.trim(bankName) == '') {
		layer.msg("Nhập tên đầy đủ is null");
		loading = false;
		return false;
	}
	if ($.trim(bankId) == '') {
		layer.msg("Ngày sinh is null");
		loading = false;
		return false;
	}
	if ($.trim(bankNo) == '') {
		layer.msg("Vui lòng chọn trình độ học vấn is null");
		loading = false;
		return false;
	}
	if ($.trim(cardFront) == '') {
		layer.msg("Tình trạng hôn nhân is null");
		loading = false;
		return false;
	}
	if ($.trim(cardBack) == '') {
		layer.msg("Vui lòng chọn số con cái is null");
		loading = false;
		return false;
	}
	if ($.trim(holdImg) == '') {
		layer.msg("Tình trạng cư trú is null");
		loading = false;
		return false;
	}

	uploadImg('file');
	uploadImg('holdImg');
	uploadImg('cardBack');

	var param = {
		bankName: bankName,
		bankCode: bankId,
		bankNo: bankNo,
		cardFront: imageFront,
		cardBack: imageBack,
		holdImg: imageHold
	}


	$.ajax({
		url: 'http://localhost:8810/loan/app/apply/saveThree',
		type: 'post',
		contentType: 'application/json',
		data: JSON.stringify(param),
		beforeSend: setHeader,
		success: function(data) {
			if (data.code == 0) {
				loading = false;
				window.location.href = "grzx4.html";
			} else {
				layer.msg(data.msg);
				loading = false;
			}
		}
	});
}

function uploadImg(fileId) {
	let formData = new FormData();
	formData.append("file", document.getElementById(fileId).files[0]);
	$.ajax({
		type: 'post',
		url: 'http://localhost:8810/loan/app/file/upload',
		processData: false,
		contentType: false,
		data: formData,
		async: false,
		beforeSend: setHeader,
		success: function(data) {
			if (data.code == 0) {
				if (fileId == 'file') {
					imageFront = data.data.src;
				} else if (fileId == 'cardBack') {
					imageBack = data.data.src;
				} else if (fileId == 'holdImg') {
					imageHold = data.data.src;
				}
			}
		},
		error: function(data, status, e) {
			layer.msg(e);
		}
	});
}
